package com.weiyi.digitaloperatingroom;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.weiyi.digitaloperatingroom.Bean.PatientInfoResult;
import com.wjr.braveheart.adapter.recyclerView.RecyclerCommonAdapter;
import com.wjr.braveheart.adapter.recyclerView.ViewHolder;
import com.wjr.braveheart.http.GoHttp;
import com.wjr.braveheart.http.callback.HttpCallback;
import com.wjr.braveheart.ui.activity.AppActivity;
import com.wjr.braveheart.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class PatientInfoActivity extends AppActivity {

    private TextView back_tv, title_tv;
    private TextView info_add_tv, info_delete_tv, info_change_tv, info_sure_tv;
    //患者信息
    private EditText patient_no_et, patient_name_et, patient_age_et, patient_sex_et, opera_name_et, opera_doctor_et, opera_time_et;
    //列表
    private RecyclerView mRecyclerView;
    private List<PatientInfoResult.DataBean> mList = new ArrayList<>();

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_patient_info;
    }

    @Override
    protected void initEventAndData(Bundle savedInstanceState, Bundle extras) {
        back_tv = getView(R.id.back_tv);
        back_tv.setOnClickListener(this);

        info_add_tv = getView(R.id.info_add_tv);
        info_add_tv.setOnClickListener(this);
        info_delete_tv = getView(R.id.info_delete_tv);
        info_delete_tv.setOnClickListener(this);
        info_change_tv = getView(R.id.info_change_tv);
        info_change_tv.setOnClickListener(this);
        info_sure_tv = getView(R.id.info_sure_tv);
        info_sure_tv.setOnClickListener(this);

        patient_no_et = getView(R.id.patient_no_et);
        patient_name_et = getView(R.id.patient_name_et);
        patient_age_et = getView(R.id.patient_age_et);
        patient_sex_et = getView(R.id.patient_sex_et);
        opera_name_et = getView(R.id.opera_name_et);
        opera_doctor_et = getView(R.id.opera_doctor_et);
        opera_time_et = getView(R.id.opera_time_et);

        mRecyclerView = getView(R.id.patient_info_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void loadData() {
        GoHttp.create(this)
                .get()
                .url(Api.GET_PATIENT_INFO)
                .addParam("PATIENT_NO", "217875")
                .execute(new HttpCallback<PatientInfoResult>() {
                    @Override
                    public void onError(Exception e) {
                        LogUtils.e(e.getMessage());
                    }

                    @Override
                    public void onSuccess(PatientInfoResult result) {
                        displayUI(result.getData());
                    }
                });
    }

    /**
     * 展示患者信息
     *
     * @param data
     */
    private void displayUI(PatientInfoResult.DataBean data) {
        patient_no_et.setText(data.getPATIENT_NO());
        patient_name_et.setText(data.getPATIENT_NAME());
        patient_age_et.setText(data.getBirthday());
        patient_sex_et.setText(data.getPATIENT_SEX() == 1 ? "男" : "女");
        opera_name_et.setText(data.getOPR_NAME());
        opera_doctor_et.setText(data.getDoctorName());
        opera_time_et.setText(data.getCreateTime());

        mList.add(data);
        mRecyclerView.setAdapter(new RecyclerCommonAdapter<PatientInfoResult.DataBean>(this, mList, R.layout.patient_info_item_adapter) {
            @Override
            protected void convert(ViewHolder holder, PatientInfoResult.DataBean item, int position) {
                holder.setText(R.id.patient_no_tv, item.getPATIENT_NO())
                        .setText(R.id.patient_name_tv, item.getPATIENT_NAME())
                        .setText(R.id.patient_sex_tv, item.getPATIENT_SEX() == 1 ? "男" : "女")
                        .setText(R.id.opera_name_tv, item.getOPR_NAME())
                        .setText(R.id.opera_time_tv, item.getCreateTime());
            }
        });

    }

    @Override
    protected void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.back_tv:
                finish();
                break;
            case R.id.info_add_tv:
                startActivity(InfoEditActivity.class);
                break;
            case R.id.info_delete_tv:
                startActivity(InfoEditActivity.class);
                break;
            case R.id.info_change_tv:
                startActivity(InfoEditActivity.class);
                break;
            case R.id.info_sure_tv:
                break;
        }
    }
}
